package com.hut.oa.controller;

import static org.mockito.Matchers.anyBoolean;

import java.util.List;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.data.repository.query.Param;
import org.springframework.data.web.PageableDefault;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hut.oa.model.bean.Announce;
import com.hut.oa.model.bean.AnnounceUser;
import com.hut.oa.model.bean.User;
import com.hut.oa.model.dao.announce.AnnounceDao;
import com.hut.oa.model.dao.announce.AnnounceUserDao;
import com.hut.oa.service.AnnounceService;

/**
 * 公告通知
 * @author 朱嘉茵
 *
 */

@Controller
@RequestMapping("/announce")
public class AnnounceController {
	@Autowired
	AnnounceDao ad;
	
	@Autowired
	AnnounceUserDao aud;
	
	@Autowired
	AnnounceService as;
	
	/**
	 * 到通知管理页面
	 * @return
	 */
	@RequestMapping
	public String toAnnounce(Model model,@PageableDefault(size=10,sort="time",direction=Direction.DESC)Pageable pageable,HttpSession session){
		User user=(User) session.getAttribute("userinfo");
		model.addAttribute("announces", ad.findByUser(user,pageable));
		return "announce/announce";
	}
	
	/**
	 * 到列表页面
	 * @param model
	 * @param pageable
	 * @param session
	 * @return
	 */
	@RequestMapping("/list")
	public String toAnnounceList(Model model,@PageableDefault(size=10,sort="announce.time",direction=Direction.DESC)Pageable pageable,HttpSession session){
		User user=(User) session.getAttribute("userinfo");
		model.addAttribute("ann", ad.findByType(0));//获取所有公告
		model.addAttribute("announces", aud.findByUser(user, pageable));//获取所有通知
		return "announce/myAnnounce";
	}
	
	/**
	 * 到表单页面
	 * @return
	 */
	@RequestMapping("/form")
	public String toAddAnnounce(@RequestParam(value="id",required=false)Long id,Model model){
		if(id!=null){
			model.addAttribute("announce", ad.findById(id));
		}
		return "announce/addAnnounce";
	}
	
	/**
	 * 保存通知
	 * @param announce
	 */
	@RequestMapping("/save")
	@ResponseBody
	public void saveAnnounce(Announce announce){
		StringBuffer sb=new StringBuffer("收到一条来自【");
		sb.append(announce.getUser().getName());
		if(announce.getAnnounceUsers()==null){
			sb.append("】的新公告");
		}else{
			sb.append("】的新通知");
		}
		as.saveAnnounce(announce,sb.toString());
	}
	
	/**
	 * 删除某条
	 * @param id
	 */
	@RequestMapping("/delete")
	@ResponseBody
	public void delete(@RequestParam("id")Long id){
		ad.delete(id);
	}
	
	/**
	 * 修改为已读
	 * @param id
	 */
	@RequestMapping("/readed")
	@ResponseBody
	public void readed(@RequestParam("id")Long id){
		as.updateRead(id);
	}
	
	/**
	 * 获取未读消息数目
	 * @param session
	 * @return
	 */
	@RequestMapping("/noread/count")
	@ResponseBody
	public int noReadCount(HttpSession session){
		User user=(User) session.getAttribute("userinfo");
		return aud.noReadCount(user);
	}
}
